package graphic.drawables;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

/**
 * Classe che rappresenta gli Sprite.
 *
 * @author Jan
 *
 */
public class Sheet {
	
	private int[][] frames;
	private int framesWidth;
	private int framesHeight;
	private int frameCount;
	
	public Sheet(String path, int nFrames, int framesWidth, int framesHeight, int columns, int rows, int hOffset, int vOffset) {
		BufferedImage img = null;
		try {
			img = ImageIO.read( new File(path) );
		} catch (IOException e) {
			e.printStackTrace();
		}
		if(img == null ){ return;}
		
		this.frameCount = nFrames;
		this.framesWidth = framesWidth;
		this.framesHeight = framesHeight;
		
		frames = new int[nFrames][framesWidth*framesHeight];
		
		int x, y;
		int index = 0;
		
		for (int j = 0; j < rows && index < nFrames; j++) {
			for (int i = 0; i < columns && index < nFrames; i++) {
				x = (framesWidth+hOffset)*i;
				y = (framesHeight+vOffset)*j;
				frames[index] = img.getRGB( x, y, framesWidth, framesHeight, null, 0, framesWidth);
				index++;
			}
		}
		

	}
	
	public int getFrameCount() {
		return frameCount;
	}
	
	public int[] getFramePixels(int index) {
		return frames[index];
	}

	public int getHeight() {
		return framesHeight;
	}
	
	public int getWidth() {
		return framesWidth;
	}
	
}
